package com.wenlin.myproduction.util;

import com.google.gson.Gson;
import com.google.gson.JsonParseException;
import com.wenlin.myproduction.bean.RosterSelectBean;
import com.wenlin.myproduction.bean.RosterSelectWithChangeBean;
import com.wenlin.myproduction.bean.ScheduleRecordBean;
import com.wenlin.myproduction.bean.ScheduleRecordListVars;
import com.wenlin.myproduction.bean.ScheduleTopBean;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author 文琳_377979485@qq.com
 * @time 2021/10/13 0013 下午 5:16
 * @desc 排班管理相关的工具类
 */
public class ScheduleUtil {

    /**
     * 获取年份月份数组
     *
     * @param yearMonth 带年字和月字的字符串
     * @return 所求
     */
    public static String[] getYearMonthArr(String yearMonth) {
        String[] arr = {"", ""};
        if (Checker.hasWord(yearMonth)) {
            if (yearMonth.contains("年")) {
                String[] arrTemp = yearMonth.split("年");
                arr[0] = arrTemp[0];
                arr[1] = arrTemp[1].replace("月", "");
            }
        }
        //如果没有获取到结果，则默认选中物理现实的当前的年月
        if (arr[0].equals("") || arr[1].equals("")) {
            String currentYearMonth = TimeUtil.getCurrentYearMonth();
            if (Checker.hasWord(currentYearMonth) && currentYearMonth.contains("-")) {
                String[] arrTemp = currentYearMonth.split("-");
                if (arrTemp.length == 2) {
                    arr[0] = arrTemp[0];
                    arr[1] = arrTemp[1];
                }
            }
        }
        return arr;
    }

    /**
     * 获取选择的时间名称所对应的参数值
     *
     * @param timeName 选择的时间名称
     * @return 所求
     */
    public static String getTimeValue(String timeName) {//timeName=本周、上一周……
        for (int i = 0; i < ValCommon.SCHEDULE_TIMES_NAMES.length; i++) {
            if (timeName.equals(ValCommon.SCHEDULE_TIMES_NAMES[i])) {
                return String.valueOf(ValCommon.SCHEDULE_TIMES_VALUES[i]);
            }
        }
        return "";
    }

    /**
     * 从字符串中解析并生成一个标题对象返回来
     *
     * @param str 字符串，带星期几、几月几号、可能带有节日名称。如：<p>星期五</p><p>10-01</p><p class="orange">国庆节</p>
     * @return 所求
     */
    public static ScheduleTopBean getScheduleTopBeanFromStr(String str) {
        //<p class="orange">星期六</p><p>10-16</p>
        //<p>星期五</p><p>10-01</p><p class="orange">国庆节</p>
        ScheduleTopBean bean = new ScheduleTopBean();
        if (Checker.hasWord(str)) {
            Pattern patternDate = Pattern.compile("(?<=>)\\d{2}-\\d{2}(?=<)");
            Matcher matcherDate = patternDate.matcher(str);
            String leftStr = str;
            String rightStr = str;
            boolean findDate = false;
            if (matcherDate.find()) {
                findDate = true;
                bean.setDate(matcherDate.group());
            }
            if (findDate) {
                String[] arr = str.split("-");
                leftStr = arr[0];
                rightStr = arr[1];
            }
            Pattern patternWeek = Pattern.compile("(?<=>)星期[一二三四五六日天]+(?=<)");
            Matcher matcherWeek = patternWeek.matcher(leftStr);
            if (matcherWeek.find()) {
                bean.setWeek(matcherWeek.group());
            }
            Pattern patternDesc = Pattern.compile("(?<=<p class=\"orange\">)\\w*(?=</p>)");
            Matcher matcherDesc = patternDesc.matcher(rightStr);
            if (matcherDesc.find()) {
                bean.setDesc(matcherDesc.group());
            }
            bean.setOrange(leftStr.contains("orange"));
        }
        return bean;
    }

    /**
     * 保存排班编辑的科室编号
     *
     * @param dId 科室编号
     */
    public static void saveEditDId(String dId) {
        SPUtils.getInstance().put(ValSp.EDIT_D_ID, dId);
    }

    /**
     * 获取排班编辑的科室编号
     *
     * @return 所求
     */
    public static String getEditDId() {
        return SPUtils.getInstance().getString(ValSp.EDIT_D_ID);
    }

    /**
     * 保存排班记录数据对象
     *
     * @param bean 排班记录数据对象
     */
    public static void saveScheduleRecordBean(ScheduleRecordBean bean) {
        SPUtils.getInstance().put(ValSp.SCHEDULE_RECORD_BEAN, new Gson().toJson(bean, ScheduleRecordBean.class));
    }

    /**
     * 获取排班记录数据对象
     *
     * @return 所求
     */
    public static ScheduleRecordBean getScheduleRecordBean() {
        ScheduleRecordBean bean = null;
        String str = SPUtils.getInstance().getString(ValSp.SCHEDULE_RECORD_BEAN);
        try {
            if (Checker.hasWord(str)) {
                bean = new Gson().fromJson(str, ScheduleRecordBean.class);
            }
        } catch (JsonParseException e) {
            e.printStackTrace();
        }
        if (Checker.isNull(bean)) bean = getFakeScheduleRecordBean();
        return bean;
    }

    /**
     * 获取一个假数据
     *
     * @return 所求
     */
    public static ScheduleRecordBean getFakeScheduleRecordBean() {
        List<ScheduleRecordBean.ListsDTO> list = new ArrayList<>();
        Random random = new Random();
        for (int i = 0; i < 50; i++) {
            List<String> listScheduleArr = new ArrayList<>();
            List<Integer> listIsNightArr = new ArrayList<>();
            List<String> listDRTArr = new ArrayList<>();
            for (int j = 0; j < 7; j++) {
                int pos = random.nextInt(ValCommon.ROSTER_TYPES.length);
                listScheduleArr.add(ValCommon.ROSTER_TYPES[pos]);
                listIsNightArr.add(pos == 1 ? 1 : 0);
                listDRTArr.add(pos <= 1 ? "12.0" : "0.0");
            }
            ScheduleRecordBean.ListsDTO newData = new ScheduleRecordBean.ListsDTO(String.valueOf(i), listScheduleArr, listIsNightArr, listDRTArr, "护士" + (i + 1));
            list.add(newData);
        }
        ScheduleRecordBean bean = new ScheduleRecordBean(list);
        saveScheduleRecordBean(bean);
        return bean;
    }

    /**
     * 保存排班记录数据对象
     *
     * @param bean 排班记录数据对象
     */
    public static void saveScheduleRecordListVars(ScheduleRecordListVars bean) {
        SPUtils.getInstance().put(ValSp.SCHEDULE_RECORD_LIST_VARS, new Gson().toJson(bean, ScheduleRecordListVars.class));
    }

    /**
     * 获取排班记录数据对象
     *
     * @return 所求
     */
    public static ScheduleRecordListVars getScheduleRecordListVars() {
        ScheduleRecordListVars bean = null;
        String str = SPUtils.getInstance().getString(ValSp.SCHEDULE_RECORD_LIST_VARS);
        try {
            if (Checker.hasWord(str)) {
                bean = new Gson().fromJson(str, ScheduleRecordListVars.class);
            }
        } catch (JsonParseException e) {
            e.printStackTrace();
        }
        return bean;
    }

    /**
     * 班次选择弹窗中选择一个班次，解析并返回一个带标签的字符串
     *
     * @param bean 某个班次
     * @return 所求
     */
    public static String getFormatScheduleWithRosterSelectBean(RosterSelectWithChangeBean bean) {
        String str = "";
        if (Checker.notNull(bean)) {
            if (Checker.notNull(bean.getBean())) {
                RosterSelectBean rsb = bean.getBean();
                str = rsb.getName();
                if (bean.isChange()) {
                    str += "<span class=\"red\">(调班)</span>";
                }
            }
        }
        return str;
    }

    /**
     * 从班次选择弹窗中返回的数据中获取是否是夜班
     *
     * @param bean 某个班次
     * @return 所求
     */
    public static int getFormatIsNight(RosterSelectWithChangeBean bean) {
        int isNight = 0;
        if (Checker.notNull(bean)) {
            if (Checker.notNull(bean.getBean())) {
                RosterSelectBean rsb = bean.getBean();
                isNight = rsb.getIs_night();
            }
        }
        return isNight;
    }

    /**
     * 从班次选择弹窗中返回的数据中获取上班时长
     *
     * @param bean 某个班次
     * @return 所求
     */
    public static String getFormatDuration(RosterSelectWithChangeBean bean) {
        String str = "";
        if (Checker.notNull(bean)) {
            if (Checker.notNull(bean.getBean())) {
                RosterSelectBean rsb = bean.getBean();
                str = rsb.getDuration();
            }
        }
        return str;
    }

}
